package com.sky.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.RedisConstant;
import com.sky.mapper.user.UserCategoryMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Category;
import com.sky.service.user.UserCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class UserCategoryServiceImpl implements UserCategoryService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserCategoryMapper userCategoryMapper;
    @Override
    public List<Category> findList() {
        String cacheList = (String)redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST);
        if (StringUtils.hasText(cacheList)){
            return JSONObject.parseArray(cacheList,Category.class);
        }
        log.info("缓存没有查询数据库");
        List<Category> list = userCategoryMapper.findList();
        redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST, JSON.toJSONString(list));
        return list;
    }
}
